package com.example.module_web.view.fragment;

import android.content.Entity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.ArrayMap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.Toast;

import com.example.module_web.R;
import com.example.module_web.beans.MonthChart;
import com.example.module_web.beans.ProductData;
import com.example.module_web.viewmodule.NetData_Web;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import Util.MPChartUtils;

import static android.content.ContentValues.TAG;
/**
 * Created by Administrator on 2018/2/19.
 */
public class Fragment_01 extends Fragment {
    CombinedChart mLineChart0;
    CombinedChart mLineChart1;
    PieChart mPieChart;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.web_fragment_01, container, false);
        mLineChart1=view.findViewById(R.id.linechart);
        mLineChart0=view.findViewById(R.id.linechart0);
        mPieChart=view.findViewById(R.id.piechart);
        initUI();
        return view;
    }
    private void initUI(){
       NetData_Web.getInstance().getYearChart(getContext(), "YearChart", new NetData_Web.Singer<String>() {
            @Override
            public void success(String s) {
                ArrayList<Entry> values=new ArrayList<>();
                ArrayList<String> names=new ArrayList<>();
                float max=0.0f;
                float min=0.0f;
                int n=0;
                JSONObject jsonObjName = null;// 得到  {"1":"颜色","2":"尺码"}对象
                try {
                    jsonObjName = new JSONObject(s);
                    // jsonObjName的key迭代器
                    Iterator<?> itName = jsonObjName.keys();// 得到 规格名称的key集合   {"1":"颜色","2":"尺码"}
                    while (itName.hasNext()) {
                        String name = itName.next().toString();
                        names.add(name);
                        String value = jsonObjName.getString(name);
                        float fvalue=Float.valueOf(value);
                        Entry _entry=new Entry(n,fvalue);
                        n++;
                        values.add(_entry);
                        if (max<fvalue) max=fvalue;
                        if (min>fvalue) min=fvalue;
                    }
                    for (int i = 0; i < names.size()-1; i++) {
                        for (int j = i+1; j < names.size(); j++) {
                            if (Integer.valueOf(names.get(i))>Integer.valueOf(names.get(j))){
                                String ss=names.get(i);
                                Entry _entry=values.get(i);
                                Entry _entry2=values.get(j);
                                _entry2.setX(i);
                                _entry.setX(j);
                                values.set(i,_entry2);
                                names.set(i,names.get(j));
                                names.set(j,ss);
                                values.set(j,_entry);
                            }

                        }
                    }
                    max=(int)((max+1000.0f)/1000)*1000;
                    MPChartUtils.configChart(mLineChart0, names,  max, min, true,Color.WHITE);
                    LineDataSet _lineDataSet = MPChartUtils.getLineData(values,"全年销售额趋势图", Color.WHITE, Color.BLUE, true);
                    MPChartUtils.initData(mLineChart0, new LineData(_lineDataSet));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void failed(String s) {
            }
        });
        NetData_Web.getInstance().getMonthChart(getContext(), "MonthChart", new NetData_Web.Singer<List<MonthChart>>() {
            @Override
            public void success(List<MonthChart> s) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
                // 1.配置基础图表配置

                double max=0.0;
                double min=0.0;
                int n=-1;
                ArrayList<ILineDataSet> _lineDataSets=new ArrayList<>();
                for (MonthChart monthChart :
                        s) {
                    n++;
                    Field[] _fields=monthChart.getClass().getDeclaredFields();
                    Map<String,Double> _map=new HashMap<>();

                        for (Field _field :
                                _fields) {
                            Log.e(TAG, "success: "+_field.getName() );

                            String  name=_field.getName();
                            name = name.substring(0,1).toUpperCase()+name.substring(1); //将属性的首字符大写，方便构造get，set方法
                            String type =_field.getGenericType().toString();    //获取属性的类型
//
                            if(type.equals("class java.lang.Double")){   //如果type是类类型，则前面包含"class "，后面跟类名
                                Method m = monthChart.getClass().getMethod("get"+name, new Class[] {});

                                Double value = (Double) m.invoke(monthChart, new Object[] {});    //调用getter方法获取属性值
                                if(value != null){
                                    _map.put(_field.getName(),value);
                                    if (max<value.doubleValue()){
                                        max=value.doubleValue();
                                    }
                                    if (min>value.doubleValue()){
                                        min=value.doubleValue();
                                    }
                                    Log.e(TAG, "success: "+String.valueOf(value.doubleValue()) );
                                }
                            }
                        }
                    ArrayList<Entry> _listdatas= new ArrayList<>();
                    _listdatas.add(new Entry(0,(float)(_map.get("One").doubleValue())));
                    _listdatas.add(new Entry(1,(float)(_map.get("Two").doubleValue())));
                    _listdatas.add(new Entry(2,(float)(_map.get("Three").doubleValue())));
                    _listdatas.add(new Entry(3,(float)(_map.get("Four").doubleValue())));
                    _listdatas.add(new Entry(4,(float)(_map.get("Five").doubleValue())));
                    _listdatas.add(new Entry(5,(float)(_map.get("Six").doubleValue())));
                    _listdatas.add(new Entry(6,(float)(_map.get("Seven").doubleValue())));
                    _listdatas.add(new Entry(7,(float)(_map.get("Eight").doubleValue())));
                    _listdatas.add(new Entry(8,(float)(_map.get("Nine").doubleValue())));
                    _listdatas.add(new Entry(9,(float)(_map.get("Ten").doubleValue())));
                    _listdatas.add(new Entry(10,(float)(_map.get("Eleven").doubleValue())));
                    _listdatas.add(new Entry(11,(float)(_map.get("Twelve").doubleValue())));
                LineDataSet _lineDataSet = MPChartUtils.getLineData(_listdatas, n==0?"去年月度销售额趋势图":"今年月度销售额趋势图", Color.WHITE, n==0?Color.BLUE:Color.parseColor("#87CEFA"), true);
                    _lineDataSets.add(_lineDataSet);
                }
                ArrayList<String> _listnames= new ArrayList<>();
                _listnames.add("One");
                _listnames.add("Two");
                _listnames.add("Three");
                _listnames.add("Four");
                _listnames.add("Five");
                _listnames.add("Six");
                _listnames.add("Seven");
                _listnames.add("Eight");
                _listnames.add("Nine");
                _listnames.add("Ten");
                _listnames.add("Eleven");
                _listnames.add("Twelve");
                max=(int)((max+1000.0f)/1000)*1000;
                MPChartUtils.configChart(mLineChart1, _listnames, (float) max,  (float)min, true,Color.WHITE);
//// 2,获取数据Data，这里有2条曲线
//                LineDataSet tartgetDataSet = MPChartUtils.getLineData(targetEntries, "目标体重", Color.WHITE, Color.WHITE, false);
//                LineDataSet lineDataSet = MPChartUtils.getLineData(weightEntries, "当前体重", Color.WHITE, getResources().getColor(R.color.chart_dot_color), false);
////  3,初始化数据并绘制
                MPChartUtils.initData(mLineChart1, new LineData(_lineDataSets));
//                Toast.makeText(getContext(), String.valueOf(s.size()), Toast.LENGTH_SHORT).show();

                }


            @Override
            public void failed(String s) {

            }
        });
        NetData_Web.getInstance().getBuyerLabel(getContext(), "BuyerLabel", new NetData_Web.Singer<String>() {
            @Override
            public void success(String s) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, JSONException {
                JSONObject _object=new JSONObject(s);
                Iterator<?> it=_object.keys();
                String name;
                ArrayList<String> names=new ArrayList<>();
                ArrayList<PieEntry> entries = new ArrayList<PieEntry>();

                while (it.hasNext()){
                     name=(String)it.next().toString();
                     names.add(name);
                     float value=Float.valueOf(_object.getString(name));
                    entries.add(new PieEntry(value, name));
                }
                MPChartUtils.configPieCHart(Color.WHITE,Color.WHITE,mPieChart,entries);
            }
            @Override
            public void failed(String s) {

            }
        });
        /**
        NetData_Web.getInstance().getProductData(getContext(), "GetProductData", "补水套盒", new NetData_Web.Singer<List<ProductData>>() {
            @Override
            public void success(List<ProductData> s) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, JSONException {

                ArrayList<Integer> _arrayLists0=new ArrayList<>();
                ArrayList<Integer> _arrayLists1=new ArrayList<>();
                ArrayList<Integer> _arrayLists2=new ArrayList<>();
                ArrayList<Integer> _arrayLists3=new ArrayList<>();
                ArrayList<Double> _arrayList=new ArrayList<>();
                ArrayList<String> _listname=new ArrayList<>();
                for (ProductData productdata :
                        s) {
                    _listname.add(productdata.getDate().substring(productdata.getDate().length() - 2));
                    _arrayLists0.add(productdata.getFangke());
                    _arrayLists1.add(productdata.getLiulan());
                    _arrayLists2.add(productdata.getJiagou());
                    _arrayLists3.add(productdata.getShoucang());
                    _arrayList.add(productdata.getTingliu());
                }

            }

            @Override
            public void faile(String s) {

            }
        });
    */
     }
}
